package top.xiangqifu.springboot2_auth.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.xiangqifu.springboot2_auth.base.BasePageResult;
import top.xiangqifu.springboot2_auth.base.BaseResult;
import top.xiangqifu.springboot2_auth.dao.TbUserMessageReplyDAO;
import top.xiangqifu.springboot2_auth.entity.AuthInfo;
import top.xiangqifu.springboot2_auth.entity.PageBean;
import top.xiangqifu.springboot2_auth.entity.TbUserMessageReply;
import top.xiangqifu.springboot2_auth.service.MessageReplyService;
import top.xiangqifu.springboot2_auth.utils.AuthInfoUtils;
import top.xiangqifu.springboot2_auth.vo.ReplyVo;

import java.util.List;
import java.util.Optional;

@Slf4j
@Service
@RequiredArgsConstructor
public class MessageReplyServiceImpl implements MessageReplyService {

    private final TbUserMessageReplyDAO tbUserMessageReplyDAO;

    @Override
    public BasePageResult<List<ReplyVo>> queryAll(ReplyVo replyVo, PageBean pageBean) {
        //分页处理
        pageBean = Optional.ofNullable(pageBean).orElse(new PageBean());
        //启动分页处理
        PageHelper.startPage(pageBean.getPageNumber(), pageBean.getPageSize());
        if (replyVo.getOrderBy()==1){
            List<TbUserMessageReply> list = tbUserMessageReplyDAO.queryAll1(replyVo.getReply());
            BasePageResult result = getBasePageResult(pageBean, list);
            return result;
        }
        if (replyVo.getOrderBy()==2){
            List<TbUserMessageReply> list = tbUserMessageReplyDAO.queryAll2(replyVo.getReply());
            BasePageResult result = getBasePageResult(pageBean, list);
            return result;
        }
        if (replyVo.getOrderBy()==3){
            List<TbUserMessageReply> list = tbUserMessageReplyDAO.queryAll3(replyVo.getReply());
            BasePageResult result = getBasePageResult(pageBean, list);
            return result;
        }
        if (replyVo.getOrderBy()==4){
            List<TbUserMessageReply> list = tbUserMessageReplyDAO.queryAll4(replyVo.getReply());
            BasePageResult result = getBasePageResult(pageBean, list);
            return result;
        }
        if (replyVo.getOrderBy()==5){
            List<TbUserMessageReply> list = tbUserMessageReplyDAO.queryAll5(replyVo.getReply());
            BasePageResult result = getBasePageResult(pageBean, list);
            return result;
        }
        if (replyVo.getOrderBy()==6){
            List<TbUserMessageReply> list = tbUserMessageReplyDAO.queryAll6(replyVo.getReply());
            BasePageResult result = getBasePageResult(pageBean, list);
            return result;
        }
        if (replyVo.getOrderBy()==7){
            List<TbUserMessageReply> list = tbUserMessageReplyDAO.queryAll7(replyVo.getReply());
            BasePageResult result = getBasePageResult(pageBean, list);


            return result;
        }
        List<TbUserMessageReply> list = tbUserMessageReplyDAO.queryAll8(replyVo.getReply());
        BasePageResult result = getBasePageResult(pageBean, list);
        return result;
    }

    private static BasePageResult getBasePageResult(PageBean pageBean, List<TbUserMessageReply> list) {
        PageInfo<TbUserMessageReply> pageInfo = new PageInfo<TbUserMessageReply>(list);
        pageBean.setPageInfo(pageInfo);
        BasePageResult result = new BasePageResult();
        result.setSuccess(true);
        result.setCode(200);
        result.setMessage("");
        result.setPage(pageBean);
        result.setData(list);
        return result;
    }

    @Override
    public BaseResult add(ReplyVo replyVo) {
        AuthInfo authInfo= AuthInfoUtils.authInfo();
        if (authInfo.getUserId()==null&&authInfo.getUserId().equals("".toString())){
            return BaseResult.ok("请登录");
        }
        TbUserMessageReply tbUserMessageReply = new TbUserMessageReply();
        tbUserMessageReply.setUmid(replyVo.getMessage().getUmid());
        tbUserMessageReply.setUserId(authInfo.getUserId());
        tbUserMessageReply.setInfo(replyVo.getReply().getInfo());
        int i=tbUserMessageReplyDAO.add(tbUserMessageReply);
        if (i!=1){
            return BaseResult.ok("评论发布失败");
        }
        return BaseResult.ok("评论发布成功");

    }

}
